package cn.example.databasetest;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private MyDatabaseHelper dbHelper;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 1);

        Button createDatabase = findViewById(R.id.create_db);
        Button addData = findViewById(R.id.add_data);
        Button updateData = findViewById(R.id.update_data);
        Button deleteData = findViewById(R.id.delete_data);
        Button queryData = findViewById(R.id.query_data);

        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values = new ContentValues();
                values.put("author", "Dan Venci");
                values.put("price", 14.99);
                values.put("pages", 544);
                values.put("name", "Dan Venci Code");
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.insert("Book", null, values);
                values.clear();
                values.put("author", "Li Shizhen");
                values.put("price", 17.99);
                values.put("pages", 1142);
                values.put("name", "Ben Cao Gang Mu");
                db.insert("Book", null, values);
            }
        });
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("author", "Dan Brown");
                values.put("price", 16.96);
                values.put("pages", 454);
                values.put("name", "The Da Vinci Code");
                db.update("Book", values, "id = ?", new String[]{"1"});
                values.put("author", "Dan Brown");
                values.put("price", 19.95);
                values.put("pages", 510);
                values.put("name", "The Lost Symbol");
                db.update("Book", values, "id = ?", new String[]{"2"});
            }
        });
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("Book", "pages > ?", new String[]{"500"});
            }
        });
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                Cursor book = db.query("Book", new String[]{"*"}, null, null, null, null, null);
                if(book.moveToFirst()){
                    do{
                        // 遍历Cursor对象，打印数据
                        @SuppressLint("Range") String name = book.getString(book.getColumnIndex("name"));
                        @SuppressLint("Range") String author = book.getString(book.getColumnIndex("author"));
                        @SuppressLint("Range") int pages = book.getInt(book.getColumnIndex("pages"));
                        @SuppressLint("Range") double price = book.getDouble(book.getColumnIndex("price"));
                        Log.d(TAG, "book name is " + name);
                        Log.d(TAG, "book author is " + author);
                        Log.d(TAG, "book pages is " + pages);
                        Log.d(TAG, "book price is " + price);
                    }while (book.moveToNext());
                }
                book.close();
            }
        });
    }
}
